library(effects)
library(tidyverse)
library(stargazer)
library(gridExtra)
library(ggplot2)
library(scales)

setwd("~/Dropbox/projects/inseparability/replication")

study2 <- read_csv("study2.csv")

cbPalette <- c("#999999", "#0072B2")

study2 <- study2[(study2$gc == 1 | study2$gc == 3)   & study2$fixed==2,]

#recode reference levels
study2$treatment[study2$treatment=="negative"] <- "Negative Result"
study2$treatment[study2$treatment=="positive"] <- "Positive Result"
study2$treatment <- as.factor(study2$treatment)


study2$pid <- study2$Q9
table(study2$Q9)
study2 <- study2[!is.na(study2$pid),]

#filter out pure idependents
study2 <- study2[study2$pid <=2,]

study2$pid<-recode(study2$pid, "1" = "Democrat", "2" = "Republican")
study2$pid <- as.factor(study2$pid)

study2$race <- NA
study2$race[study2[,"Q5"]==1] <- "White"
study2$race[study2[,"Q5"]==2] <- "Black"

study2$race <- as.factor(study2$race)

study2$inrace <- study2$Q852_2
study2$outrace <- study2$Q786_2
study2$outparty <- study2$Q825_2
study2$inparty <- study2$Q817_2

study2$racediff <- study2$inrace - study2$outrace
study2$partydiff <- study2$inparty - study2$outparty

######################
# Manipulation Check
######################

study2$Q199[study2$Q199==4] <- 3
study2$Q199[study2$Q199==5] <- 4
t.test(Q199~treatment, data=study2)

######################
# Average Spillover
######################

df <- data.frame(group=character(),
				 mean=double(),
				 lower=double(),
				 upper=double(),
				 stringsAsFactors=FALSE)

tspill_party <- t.test(study2$outparty~study2$treatment)
tmain <- t.test(study2$outrace~study2$treatment)

2.719298              -        4.682406 
3.165119            -          4.968191 
format_ttest <- function(df, t, n){
	df[nrow(df)+1,] <- c(n, as.numeric(t$estimate[1] - t$estimate[2]), t$conf.int[1],t$conf.int[2])
	df$mean <- as.numeric(df$mean)
	df$upper <- as.numeric(df$upper)
	df$lower <- as.numeric(df$lower)
	return(df)
	
}

df <- format_ttest (df, tspill_party, "Parallel updating:\nout-partisan affect")
df <- format_ttest (df, tmain, "Out-racial affect")
df$group <- as.factor(df$group)
df$group <- factor(df$group,levels(df$group)[c(2,3,1)])


(ate <- ggplot(df,aes(y=mean,x=group))+
		geom_point()+
		geom_linerange(aes(ymin=lower,ymax=upper))+
		theme_bw()+
		ylab("Difference in allocations to the \nout-group between negative and positive treatments")+
		xlab("") + 
		ggtitle("B: Study 2 (Racial Treatment)") +
		geom_hline(yintercept=0, linetype="dashed") + 
		theme(panel.spacing = unit(1, "lines")) + 
		theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"), axis.title.x = element_text(vjust=-0.5)) +
		scale_y_continuous(labels=dollar_format(prefix="$"), limits=c(-3,.2)) +

		coord_flip())

ggsave(plot=ate,"study2_ate.eps", device="eps",width=5, height=1.75)

t.test(study2$outrace~study2$treatment)
3.165119           -           4.968191
t.test(study2$inrace~study2$treatment)
3.863777           -           5.065109
t.test(study2$outparty~study2$treatment)
2.719298           -           4.682406 
t.test(study2$inparty~study2$treatment)
3.370485           -           4.769384

1.80/10

######################x
# Diff-in-diff
######################
t.test(study2$inparty~study2$treatment)
t.test(study2$outparty~study2$treatment)
t.test(study2$partydiff~study2$treatment)
0.65118679          -          0.08697813 
t.test(study2$racediff~study2$treatment)
0.69865841          -          0.09691849 

######################
# Out-group activation
######################

aligned <- lm(outparty~treatment, data= study2[(study2$race=="Black" & study2$pid == "Democrat") | (study2$race!="Black" & study2$pid == "Republican") ,])
confint(aligned)
not <- lm(outparty~treatment, data= study2[(study2$race=="Black" & study2$pid == "Republican") | (study2$race!="Black" & study2$pid == "Democrat"),])
confint(not)

study2$aligned <- "not"
study2$aligned[(study2$race=="Black" & study2$pid == "Democrat") | (study2$race!="Black" & study2$pid == "Republican")] <- "yes"

stargazer(aligned, not, covariate.labels=c("Positive Treatment"), dep.var.labels = c("Out Party Affect"), omit.stat = c("f", "ser"), ci=T, star.cutoffs = c(0.05, 0.01, 0.001), type="latex", column.labels = c("Aligned Out-Groups", "Unaligned Out-Groups"))

######
# Halo
######

df <- data.frame(group=character(),
				 mean=double(),
				 lower=double(),
				 upper=double(),
				 stringsAsFactors=FALSE)

partydiff <- t.test(study2$partydiff~study2$treatment)
0.65118679          -          0.08697813 
racediff <- t.test(study2$racediff~study2$treatment)
0.69865841          -          0.09691849 

df <- format_ttest (df, partydiff, "Party Difference")
df <- format_ttest (df, racediff, "Race Difference")
df$group <- as.factor(df$group)
df$group <- factor(df$group,levels(df$group)[c(2,3,1)])
df

###########
# Mechanism
###########

df <- data.frame(group=character(),
				 mean=double(),
				 lower=double(),
				 upper=double(),
				 stringsAsFactors=FALSE)

op_aligned <-t.test(outparty~treatment, data=study2[study2$aligned == "yes",])
2.675781         -             4.627024
op_unaligned <- t.test(outparty~treatment, data=study2[study2$aligned != "yes",])
2.885572     -                 4.901478

df <- format_ttest (df, op_aligned, "Sorted Out-Groups")
df <- format_ttest (df, op_unaligned, "Cross-Cutting Out-Groups")
df$group <- as.factor(df$group)
df$group <- factor(df$group,levels(df$group)[c(2,3,1)])

df

(study2_mech <- ggplot(df,aes(y=mean,x=group))+
		geom_point()+
		geom_linerange(aes(ymin=lower,ymax=upper))+
		ylim(-3,0)+
		theme_bw()+
		ylab("(Dollars)")+
		xlab("") + 
		ggtitle("B: Study 2 (Racial Treatment)") +
		geom_hline(yintercept=0, linetype="dashed") + 
		theme(panel.spacing = unit(1, "lines")) + 
		theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"), axis.title.x = element_text(vjust=-0.5)) +
		coord_flip())

ggsave(plot=study2_mech,"study2_mech.eps", device="eps",width=5, height=1.2)


####################
# HET
####################

white <- lm(outparty~treatment,data = study2[study2$race == "White",])
black <- lm(outparty~treatment,data = study2[study2$race != "White",])

democrat <- lm(outparty~treatment, data = study2[study2$pid == "Democrat",])
republican  <- lm(outparty~treatment, data = study2[study2$pid != "Democrat",])

stargazer(white, black, democrat, republican, covariate.labels=c("Positive Treatment"), dep.var.labels = c("Spillover to Out Party Affect"), omit.stat = c("f", "ser"), ci=T, star.cutoffs = c(0.05, 0.01, 0.001), column.labels = c("Whites", "Blacks", "Democrats", "Republicans"), type = "latex")

